library(devtools)
library(stringr)
source_url("https://gitee.com/ry2an/my_public_functions_r/raw/master/split_unique.R")
source_url("https://gitee.com/ry2an/my_public_functions_r/raw/master/count_bar_generate_y.R")

split_cate <- function(vec = c(), breaks = c()){
  pool <- split_unique(x = vec, breaks = breaks)
  nums <- rep(0, length(pool))

  for(i in 1:length(pool)){
    sum <- 0
    for(j in 1:length(vec)){
      pieces <- c(vec[j])
      for(k in 1:length(breaks)){
        piece_temp <- c()
        for(l in 1:length(pieces)){
          temp <- str_split(string = pieces[l], pattern = breaks[k])[[1]]
          piece_temp <- c(piece_temp, temp)
        }
        pieces <- piece_temp
      }
      sum <- sum + length(pieces[pieces == pool[i]])
    }
    nums[i] <- sum
  }
  out_data <- data.frame(pool, nums)
  return(out_data)
}

split_cate2 <- function(vec = c(), breaks = c(), nas = "not_avaliable"){
  vec[is.na(vec)] <- nas
  temp_df <- count_bar_generate_y(vec)
  pool <- split_unique(x = vec, breaks = breaks)
  nums <- rep(0, length(pool))
  for(i in 1:dim(temp_df)[1]){
    temp_names <- split_unique(temp_df$types[i], breaks = breaks)
    for(j in 1:length(temp_names)){
      nums[pool == temp_names[j]] <- nums[pool == temp_names[j]] + temp_df$count[i]
    }
  }
  out_data <- data.frame(pool, nums)
  return(out_data)
}



# i = 2
# 
# 
# vec = c("aaa,bbb,ccc", "ddd,ccc,aaa", "eee-bbb,aaa")
# breaks = c(",", "-")
# split_cate2(vec, breaks)




